package com.leyou.auth.Servcie.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.leyou.api.controller.FeignController;
import com.leyou.auth.Servcie.AuthService;
import com.leyou.auth.config.OssProperties;


import com.leyou.common.exceptions.LyException;
import com.leyou.pojo.dto.OssPropertiesDto;
import com.leyou.pojo.dto.UserDetail;
import com.leyou.pojo.dto.UserDto;
import com.leyou.pojo.util.JwtUtils;
import feign.FeignException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import static com.leyou.common.constants.JwtConstants.COOKIE_NAME;
import static com.leyou.common.constants.JwtConstants.DOMAIN;

@Service
public class AuthServiceImpl implements AuthService {



    @Autowired
    private OssProperties ossProperties;

    private OssPropertiesDto ossPropertiesDto;

    @Autowired
    private OSS ossClient;


    /**
     *
     * @return
     */
    @Override
    public OssPropertiesDto uploadFile() {
        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, ossProperties.getDir());

            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);

//            Map<String, String> respMap = new LinkedHashMap<String, String>();
//            respMap.put("accessid", accessId);
//            respMap.put("policy", encodedPolicy);
//            respMap.put("signature", postSignature);
//            respMap.put("dir", dir);
//            respMap.put("host", host);
//            respMap.put("expire", String.valueOf(expireEndTime / 1000));

            //拼装返回信息
//             ossPropertiesDto = new OssPropertiesDto();
//            ossPropertiesDto.setAccessId(ossProperties.getAccessKeyId());
//            ossPropertiesDto.setDir(ossProperties.getDir());
//            ossPropertiesDto.setExpire(expireEndTime);
//            ossPropertiesDto.setHost(ossProperties.getHost());
//            ossPropertiesDto.setPolicy(encodedPolicy);
//            ossPropertiesDto.setSignature(postSignature);

            ossPropertiesDto =  OssPropertiesDto.of(ossProperties.getAccessKeyId(),ossProperties.getHost(),
                    encodedPolicy,postSignature, ossProperties.getExpireTime(),ossProperties.getDir());

            // respMap.put("expire", formatISO8601Date(expiration));
            //回调
//            JSONObject jasonCallback = new JSONObject();
//            jasonCallback.put("callbackUrl", callbackUrl);
//            jasonCallback.put("callbackBody",
//                    "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
//            jasonCallback.put("callbackBodyType", "application/x-www-form-urlencoded");
//            String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes());
//            respMap.put("callback", base64CallbackBody);
//
//            JSONObject ja1 = JSONObject.fromObject(respMap);
            // System.out.println(ja1.toString());
//            response.setHeader("Access-Control-Allow-Origin", "*");
//            response.setHeader("Access-Control-Allow-Methods", "GET, POST");
//            response(request, response, ja1.toString());

        } catch (Exception e) {
            // Assert.fail(e.getMessage());
//            System.out.println(e.getMessage());
            throw new LyException(e.getMessage(),e,500);

        }
        return ossPropertiesDto;
    }


}
